package com.atguigu.rabbitmq.three;

import com.atguigu.rabbitmq.utils.RabbitMqUtils;
import com.atguigu.rabbitmq.utils.SleepUtils;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

import java.nio.charset.StandardCharsets;

/**
 * 消息在手动应答时是不丢失、放回队列中重新消费
 * @author 连志阳
 * @since 2022/2/8
 */
public class Work04 {
    //队列名称
    public static final String TASK_QUEUE_NAME = "ack_queue";
    
    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        System.out.println("C2等待接收消息处理时间比较长");
    
        DeliverCallback deliverCallback = (consumerTag, message) -> {
            //沉睡30s
            SleepUtils.sleep(30);
            System.out.println("接收到消息：" + new String(message.getBody(), StandardCharsets.UTF_8));
            /*
            手动应答
            1.消息的标记tag
            2.是否批量应答，false：不批量应答，true：批量
             */
            channel.basicAck(message.getEnvelope().getDeliveryTag(), false);
        };
        CancelCallback cancelCallback = consumerTag -> System.out.println(consumerTag + "消费者取消消费接口回调逻辑");
        //设置不公平分发
        //int prefetchCount = 1;
        //预取值5
        int prefetchCount = 5;
        channel.basicQos(prefetchCount);
        //采用手动应答
        channel.basicConsume(TASK_QUEUE_NAME, false, deliverCallback, cancelCallback);
    }
}
